package com.violet.myorm.core.source;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class Source {
    private static final DataSource dataSource;

    static {
        try (InputStream stream = ClassLoader.getSystemResourceAsStream("myorm.properties")) {
            Properties properties = new Properties();
            properties.load(stream);

            dataSource = DruidDataSourceFactory.createDataSource(properties);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        }
        catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
